package net.maku.bean.model;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import com.fasterxml.jackson.databind.ObjectMapper;

public enum RespStatus {

	SUCCESS( 200,  "请求成功" ),
	FAIL(    500,  "请求失败" ),
	NOTURL(  404,  "访问地址不存在" );
	
	private int 	code;
	private String 	msg;
	
	
	RespStatus( int code, String msg ){
		this.code = code;
		this.msg  = msg;
	}

	public int getCode() {
		return code;
	}

	public String getMsg() {
		return msg;
	}
	
	public ResponseEntity<String> getRespone() {
		return new ResponseEntity<String>( this.getMsg() , 
				HttpStatus.resolve( this.getCode() ) );
	}

	public ResponseEntity<String> getRespone( String msg ) {
		if( StringUtils.isBlank(msg) ) {
			return getRespone();
		}
		return new ResponseEntity<String>( msg, 
				HttpStatus.resolve( this.getCode() ) );
	}
	
	public ResponseEntity<String> getRespone( Object bean ) throws Exception {
		if( bean!=null ) {
			ObjectMapper objectMapper = new ObjectMapper();
			//msg = JSON.toJSONString(bean);
			msg = objectMapper.writeValueAsString(bean);
		}
		return new ResponseEntity<String>( msg, 
				HttpStatus.resolve( this.getCode() ) );
	}
	
	
	public static void main(String[] args) {
		
		ObjectMapper objectMapper = new ObjectMapper();
        String carJson = "{ \"brand\" : \"Mercedes\", \"doors\" : 6 }";
        try {
        	Map car = objectMapper.readValue(carJson, Map.class);
            System.out.println(car);
            
            String msg = objectMapper.writeValueAsString(car);
            System.out.println(msg);
        } catch ( Exception e) {
            e.printStackTrace();
        }
	}
}


